package com.tiger.cloud.controller.crm;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.CrmDeleteEnum;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.model.crm.AppVersion;
import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.share.QueryShareReturn;
import com.tiger.cloud.model.share.ShareParamater;
import com.tiger.cloud.model.share.ShareQuery;
import com.tiger.cloud.model.share.ShareRecipientLog;
import com.tiger.cloud.service.crm.IAppVersionService;
import com.tiger.cloud.service.crm.IUserService;
import com.tiger.cloud.service.share.IShareRecipientLogService;
import com.tiger.cloud.service.share.IShareService;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("share")
public class ShareController extends BaseController {

    @Autowired
    private IShareService shareService;

    @Autowired
    private IUserService crmUserService;

    @Autowired
    private IShareRecipientLogService shareRecipientLogService;

    @Autowired
    private IAppVersionService appVersionService;

    @IgnoreAuth
    @RequestMapping("getVCode")
    @CrossOrigin
    @ResponseBody
    public Response getVCode(@RequestBody ShareQuery query, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        if (query.getMobile() == null || query.getMobile().equals("")) {
            return ResponseUtil.error(ErrorCode.SHARE_MOBILE_NUL);
        }

        //先验证手机号是否注册,是否已参加过活动start
        Map<String, Object> queryUserMap = new HashMap<>();
        queryUserMap.put("loginId", query.getMobile());
        queryUserMap.put("isDelete", CrmDeleteEnum.NOMAIL.getCode());
        User user = crmUserService.findOne(queryUserMap);
        if (user != null) {
            return ResponseUtil.error(ErrorCode.SHARE_MOBILE_HAS);
        }

        Map<String, Object> queryShareMap = new HashMap<>();
        queryShareMap.put("recipientMobile", query.getMobile());
        ShareRecipientLog one = shareRecipientLogService.findOne(queryShareMap);
        if (one != null) {
            return ResponseUtil.error(ErrorCode.SHARE_MOBILE_HAVE);
        }
        //先验证手机号是否注册,是否已参加过活动end

        //验证五分钟之内不能重发验证码start
        Map<String, Object> queryShareSmsMap = new HashMap<>();
//        queryShareSmsMap.put("",);
//        queryShareSmsMap.put("",);
//        queryShareSmsMap.put("",);
//
//        shareSmsService.find()

        Boolean flg = shareService.getVCode(query);
        if (flg) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.error(ErrorCode.INTERNAL_ERROR, "服务器正在吃鸡,不想睬你,并扔了个异常");
    }


    /**
     * 验证验证码并记录接收人
     *
     * @param query
     * @return
     */
    @IgnoreAuth
    @RequestMapping("operate")
    @CrossOrigin
    @ResponseBody
    public Response operate(@RequestBody ShareQuery query) {
        if (query.getMobile() == null || query.getMobile().equals("")) {
            return ResponseUtil.error(ErrorCode.SHARE_MOBILE_NUL);
        }

        if (query.getCode() == null || query.getCode().equals("")) {
            return ResponseUtil.error(ErrorCode.SHARE_CODE_NULL);
        }

        //先验证手机号是否注册,是否已参加过活动start
        Map<String, Object> queryUserMap = new HashMap<>();
        queryUserMap.put("loginId", query.getMobile().toString());
        queryUserMap.put("isDelete", CrmDeleteEnum.NOMAIL.getCode());
        User user = crmUserService.findOne(queryUserMap);
        if (user != null) {
            return ResponseUtil.error(ErrorCode.SHARE_MOBILE_HAS);
        }

        Map<String, Object> queryShareMap = new HashMap<>();
        queryShareMap.put("recipientMobile", query.getMobile().toString());
        ShareRecipientLog one = shareRecipientLogService.findOne(queryShareMap);
        if (one != null) {
            return ResponseUtil.error(ErrorCode.SHARE_MOBILE_HAVE);
        }
        //此手机号有无验证码
        Boolean hasMobile = shareService.hasMobile(query);
        if (hasMobile) {
            return ResponseUtil.error(ErrorCode.SHARE_MOBILE_NO_CODE);
        }

        //验证验证码时间
        Boolean timeFlg = shareService.verifyCodeTime(query);   //false是没过期,true过期
        if (timeFlg) {
            return ResponseUtil.error(ErrorCode.SHARE_CODE_OUT);
        }


        //验证验证码
        Boolean verify = shareService.verifyCode(query);
        if (!verify) {
            return ResponseUtil.error(ErrorCode.SHARE_CODE_ERROR);
        }

        //验证分享人
        User crmUser = shareService.shareId(query);
        if (crmUser == null) {
            return ResponseUtil.error(ErrorCode.SHARE_NO_SHARER);
        }

        //插入分享接收记录
        Boolean flg = shareService.insertRecord(query, crmUser);

        if (flg) {
            List<AppVersion> appVersions = appVersionService.give();    //id为1,6,7(测试服),123为正式服

            return ResponseUtil.ok(appVersions);
        }
        return ResponseUtil.error();
    }


    /**
     * 获取分享的基数和倍数
     *
     * @return
     */
    @IgnoreAuth
    @RequestMapping("getParamater")
    @CrossOrigin
    @ResponseBody
    public Response getParamater() {
        List<ShareParamater> shareParamater = shareService.getParamater();
        return ResponseUtil.ok(shareParamater);
    }


    /**
     * 查找最新版本的APP,安卓
     */
//    @RequestMapping(value = "findNewVersion")
//    @ResponseBody
//    @IgnoreAuth
//    public Response findNewVersion() {              //@RequestBody Integer client
//        List<AppVersion> appVersions = appVersionService.give();    //id为1,6,7(测试服),123为正式服
//
//        return ResponseUtil.ok(appVersions);
//    }
    @RequestMapping(value = "findNewVersion", method = RequestMethod.POST)
    @ResponseBody
    @IgnoreAuth
    public Response findNewVersion() {
        List<AppVersion> appVersions = new ArrayList<>();

        AppVersion appVersion = new AppVersion();
        appVersion.setClient(0);
        appVersion.setStartUseTime(new Timestamp(System.currentTimeMillis()));
        //appVersion.setOrderBy("APP_VERSION.start_use_time desc");
        appVersion.setPageIndex(0);
        appVersion.setPageSize(1);
        AppVersion android = appVersionService.findNewVersion(appVersion);        //获取安卓


        AppVersion ios = appVersionService.get(1l);
        AppVersion pc = android;

        appVersions.add(android);
        appVersions.add(ios);
        appVersions.add(pc);

        return ResponseUtil.ok(appVersions);
    }

    /**
     * 查询自己接收分享的分享着,和接收自己分享的被分享者(正常用户,状态为1)
     *
     * @return
     */
    @RequestMapping(value = "queryShareMyself")
    @ResponseBody
    @CrossOrigin
    @IgnoreAuth
    public Response queryShareMyself(@RequestBody User crmUser) {
        //crmUser.id接收的是当前用户的id
        if (crmUser.getId() == null || crmUser.getId() < 1) {
            return ResponseUtil.error(ErrorCode.PARAMETER_INVALID);
        }
        QueryShareReturn queryShareReturn = shareService.queryShareMyself(crmUser);

        return ResponseUtil.ok(queryShareReturn);
    }


    /**
     * shareCode查询用户
     *
     * @return
     */
    @RequestMapping(value = "findByShareCode")
    @ResponseBody
    @CrossOrigin
    @IgnoreAuth
    public Response findByShareCode(@RequestBody User crmUser) {
        if (crmUser.getShareCode() == null){
            return ResponseUtil.error(ErrorCode.PARAMETER_INVALID);
        }
        User one = crmUserService.findOne(crmUser);

        return ResponseUtil.ok(one);
    }

}
